<?php

namespace App\Admin\Http\Controllers\Web;

use App\Admin\Http\Requests\AdRequest;
use Flash;
use App\Admin\Eloquent\Ad;
use App\Exceptions\BusinessException;
use App\Admin\Http\Controllers\Controller;
use App\Extensions\Pagination\BootstrapThreePresenter;
use Illuminate\Http\Request;


class AdsController extends Controller
{
  /**
   * 广告列表
   *
   * @param Request $request
   * @param int $per_page
   *  @return \Illuminate\Http\Response
   */
  public function index(Request $request)
  {
    $ad_position = $request->input('ad_position');

    $query = Ad::query();
    if(!empty($ad_position))
      $query = $query->where('position',$ad_position);

    $ads = $query->orderBy('created_at', 'desc')->paginate(15);
    if(!empty($ad_position))
      $ads->appends(['ad_position' => $ad_position]);

    $positions = Ad::getPositions();

    // 分页
    $links = (new BootstrapThreePresenter($ads))->render();

    return view('admin.web.ads.index', compact('ads', 'positions', 'ad_position', 'links'));
  }

  /**
   * 新增广告表单
   *
   * @return \Illuminate\Http\Response
   */
  public function create()
  {
    $positions = Ad::getPositions();

    return view('admin.web.ads.create', compact('positions'));
  }

  /**
   * 新增广告表单
   *
   * @param AdRequest $request
   * @return \Illuminate\Http\RedirectResponse
   */
  public function store(AdRequest $request)
  {
    $arr =$request->file('image');
    if(!empty($arr)) {
      //处理图片
      $json = upload_file($arr);    //上传文件
      if ($json['code'] == 10000) {
        $arr = "{$json['file']['full_url']}";


      } else {
        throw new BusinessException('文件上传失败:' . $json['message']);
      }
    }
      $ad = new Ad($request->input());

       $ad->image =$arr;

       $ad->save();

       Flash::success('新增广告记录成功');

      return $this->redirectAction('Web\AdsController@index');
//    if ( $arr['code']==10000 ) {
//      $ad = new Ad($request->input());
//
//      $ad->image = "{$arr['file']['full_url']}/{$arr['file']['filename']}";
//
//      $ad->save();
//
//      Flash::success('新增广告记录成功');
//
//      return $this->redirectAction('Web\AdsController@index');
//    } else {
//      $message = $arr['message'];
//
//      return back()->withInput()->with('error', "新增失败，$message");
//    }
  }
  /**
   * 根据id删除广告
   *
   * @param int $id
   * @return \Illuminate\Http\Response
   */
  public function destroy($id)
  {
    $ad = Ad::findOrFail($id);
    $ad->delete();

    Flash::success('删除广告成功');

    return $this->redirectAction('Web\AdsController@index');
  }

  /**
   * 编辑广告表单
   *
   * @param int $id
   * @return \Illuminate\Http\Response
   */
  public function edit(int $id)
  {
    $ad = Ad::findOrFail($id);

    $positions = Ad::getPositions();

    return view('admin.web.ads.edit', compact('ad', 'positions'));
  }

  /**
   * 修改广告表单
   *
   * @param AdRequest $request
   * @param int $id
   * @return \Illuminate\Http\Response
   * @throws BusinessException
   */
  public function update(AdRequest $request,int $id)
  {
    $arr = $request->input();
    $ad =  Ad::findOrFail($id);
    //如果图片不为空
    if($image = $request->file('image')) {
      //处理图片
      $json = upload_file($image);    //上传文件
      if ($json['code'] == 10000) {
        $arr['image'] = "{$json['file']['full_url']}";
      } else {
        throw new BusinessException('文件上传失败:' . $json['message']);
      }
    }
    $ad->update($arr);

    Flash::success('修改广告成功');

    return $this->redirectAction('Web\AdsController@index');
  }

  /**
   * 根据id查看广告
   *
   * @param int $id
   * @return \Illuminate\Http\Response
   */
  public function show(int $id)
  {
    $ad = Ad::findOrFail($id);

    return view('admin.web.ads.show', compact('ad'));
  }

}